package com.example.demo;

import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;

@Service
public class WeatherService {

    private final RestClient restClient;

    public WeatherService() {
        this.restClient = RestClient.builder()
                .baseUrl("https://api.weather.gov")
                .defaultHeader("Accept", "application/geo+json")
                .defaultHeader("User-Agent", "WeatherApiClient/1.0 (your@email.com)")
                .build();
    }

    @Tool(description = "Get weather forecast for a specific latitude/longitude")
    public String getWeatherForecastByLocation(
            double latitude,   // Latitude coordinate
            double longitude   // Longitude coordinate
    ) {
        // Returns detailed forecast including:
        // - Temperature and unit
        // - Wind speed and direction
        // - Detailed forecast description
        
        // 首先获取点位信息，Weather.gov API需要先获取点位信息才能获取预报
         ForecastPointResponse pointResponse = restClient.get()
                .uri("/points/{latitude},{longitude}", latitude, longitude)
                .retrieve()
                .body(ForecastPointResponse.class);
        

        String forecastUrl = pointResponse.getProperties().getForecast();
        
        // 获取预报数据
        String forecastResponse = restClient.get()
                .uri(forecastUrl)
                .retrieve()
                .body(String.class);
        
        // 解析预报数据并提取所需信息
        StringBuilder result = new StringBuilder();
        result.append("Weather Forecast:\n");
        
        // 提取温度信息
        int tempStart = forecastResponse.indexOf("\"temperature\":") + 14;
        int tempEnd = forecastResponse.indexOf(",", tempStart);
        String temperature = forecastResponse.substring(tempStart, tempEnd);
        
        // 提取温度单位
        int tempUnitStart = forecastResponse.indexOf("\"temperatureUnit\":") + 18;
        int tempUnitEnd = forecastResponse.indexOf("\"", tempUnitStart + 1);
        String temperatureUnit = forecastResponse.substring(tempUnitStart + 1, tempUnitEnd);
        
        // 提取风速和方向
        int windSpeedStart = forecastResponse.indexOf("\"windSpeed\":") + 12;
        int windSpeedEnd = forecastResponse.indexOf("\"", windSpeedStart + 1);
        String windSpeed = forecastResponse.substring(windSpeedStart + 1, windSpeedEnd);
        
        int windDirectionStart = forecastResponse.indexOf("\"windDirection\":") + 16;
        int windDirectionEnd = forecastResponse.indexOf("\"", windDirectionStart + 1);
        String windDirection = forecastResponse.substring(windDirectionStart + 1, windDirectionEnd);
        
        // 提取详细描述
        int detailedForecastStart = forecastResponse.indexOf("\"detailedForecast\":") + 19;
        int detailedForecastEnd = forecastResponse.indexOf("\"", detailedForecastStart + 1);
        String detailedForecast = forecastResponse.substring(detailedForecastStart + 1, detailedForecastEnd);
        
        // 构建结果
        result.append("Temperature: ").append(temperature).append(" ").append(temperatureUnit).append("\n");
        result.append("Wind: ").append(windSpeed).append(" from ").append(windDirection).append("\n");
        result.append("Forecast: ").append(detailedForecast).append("\n");
        
        return result.toString();
    }

    @Tool(description = "Get weather alerts for a US state")
    public String getAlerts(
            @ToolParam(description = "Two-letter US state code (e.g. CA, NY)") String state
    ) {
        // Returns active alerts including:
        // - Event type
        // - Affected area
        // - Severity
        // - Description
        // - Safety instructions
        return "Weather Alerts:\n" +
                "- Event: Flood Watch\n" +
                "- Affected Area: San Francisco Bay Area\n" +
                "- Severity: Warning\n" +
                "- Description: Flood warning issued for the San Francisco Bay Area.\n" +
                "- Safety Instructions: Stay away from the affected area until the warning expires.";
    }

    // ......
}
